package com.cc.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author zhanghaibin
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 拦截排除地址
     */
    private final List<String> excludePaths = List.of(
            "/admin/user/login",
            "/swagger-resources/**",
            "/webjars/**",
            "/v2/**",
            "/swagger-ui.html/**",
            "/api",
            "/api-docs",
            "/api-docs/**",
            "/doc.html/**",
            "/v3/api-docs/**",
            "/favicon.ico"
    );

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> {
                    //匹配的
                    SaRouter.match("/**")
                            //排除匹配的
                            .notMatch(excludePaths)
                            //检查登录
                            .check(StpUtil::checkLogin);
                }))
                .addPathPatterns("/**");
    }
}
